package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sql.DBManagment;
import sql.record.*;
/**
 * Servlet implementation class login
 */
public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User loginUser = new User();
		loginUser.userName = request.getParameter("username");
		PrintWriter out = response.getWriter();
		try {
			List<User> userList = DBManagment.getRecord(loginUser);
			User storedUser = userList.get(0);
			if (storedUser.password.equals(request.getParameter("password"))){
				request.getSession().setAttribute("loggedUser", loginUser.userName);
				out.print("<firefoxsucks>loggedin</firefoxsucks>");
			}else{
				out.print("<firefoxsucks>unloggedin</firefoxsucks>");
			}
		} catch (Exception e) {
			out.print("<firefoxsucks>unloggedin</firefoxsucks>");
		}
		out.close();
	}

}
